package stack;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.HashMap;
import java.util.Map;

public class P316 {
}

class P316Test01{
    Deque<Character> stack;

    public String removeDuplicateLetters(String s) {
        stack=new ArrayDeque<>();
        for(int i=0;i<s.length();i++){
            Character c=s.charAt(i);
            if(stack.contains(c)){
                continue;
            }
            //s.indexOf(stack.peek(),i)!=-搜索i位置以后的字符串是否还有栈顶元素，有的话直接退栈，没有的话就进栈，保证最小字典序
            while(!stack.isEmpty()&&stack.peek()>c&&s.indexOf(stack.peek(),i)!=-1)
                stack.poll();
            stack.push(c);
        }
        String s1="";
        for(Character a:stack){
            s1=a+s1;
        }
        return s1;
    }
}
